package controller;

import entity.Students;
import mapper.StudentMapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class StudentController {
//自动查找bean，并注入
    @Autowired
//@Autowired注解可能无法确定要注入哪个Bean，
// 这时可以结合@Qualifier注解来指定具体的Bean。
// @Qualifier注解通常和@Autowired注解一起使用，通过指定Bean的名称来实现精确的注入。
    @Qualifier("studentMapperImpl")
    private StudentMapperImpl studentMapper;
//@requestMapping是映射请求路径的
    @RequestMapping("/t1")
    //Model本质是一个map，键值对存储数据，向视图返回数据
    public String test01(Model model, HttpServletRequest request) {
        int id;
        id = Integer.parseInt(request.getParameter("id"));
        Students students = studentMapper.getStudentById(id);
        String q = students.getName();
        model.addAttribute("a1",q);
        //返回视图名
        return "index";
    }
    @RequestMapping("/t2")
    public String test02(Model model, HttpServletRequest request) {
        Students students = new Students();
        students.setName(request.getParameter("name"));
        students.setAge(Integer.parseInt(request.getParameter("age")));
        students.setId(Integer.parseInt(request.getParameter("id")));
        studentMapper.insertStudent(students);
        String q = "插入成功";
        model.addAttribute("a2",q);
        //返回视图名
        return "index";
    }
    @RequestMapping("/t3")
    public String test03(Model model, HttpServletRequest request) {
        Students students = studentMapper.getStudentById(Integer.parseInt(request.getParameter("ID")));
        students.setName(request.getParameter("name"));
        students.setAge(Integer.parseInt(request.getParameter("age")));
        students.setId(Integer.parseInt(request.getParameter("id")));
        studentMapper.insertStudent(students);
        String q = "修改成功";
        model.addAttribute("a3",q);
        //返回视图名
        return "index";
    }
    @RequestMapping("/t4")
    public String test04(Model model, HttpServletRequest request) {
        int id;
        id = Integer.parseInt(request.getParameter("id"));
        studentMapper.deleteStudent(id);
        String q = "删除成功";
        model.addAttribute("a4",q);
        //返回视图名
        return "index";
    }
}